package com.oj.job.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oj.common.core.constants.CacheConstants;
import com.oj.common.core.enums.ExamListType;
import com.oj.common.redis.service.RedisService;
import com.oj.job.domain.Exam;
import com.oj.job.mapper.ExamMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author bin
 * @Date 2025/6/3
 */
@Component
@Slf4j
public class XxlJobs {

    @Resource
    private ExamMapper examMapper;

    @Resource
    private RedisService redisService;

    @XxlJob("examListOrganizeHandler")
    public void examListOrganizeHandler() {
        log.info("刷新竞赛列表缓存完成 examListOrganizeHandler");
        // 统计那些竞赛应该存入未完赛列表 那些应该存入历史列表
        refreshCache(ExamListType.EXAM_UN_FINISH_LIST.getValue());
        refreshCache(ExamListType.EXAM_HISTORY_LIST.getValue());
    }

    public void refreshCache(Integer examListType) {
        List<Exam> examList = new ArrayList<>();
        if (ExamListType.EXAM_UN_FINISH_LIST.getValue().equals(examListType)) {
            //查询未完赛的竞赛列表
            examList = examMapper.selectList(new LambdaQueryWrapper<Exam>()
                    .select(Exam::getExamId, Exam::getTitle, Exam::getStartTime, Exam::getEndTime)
                    .gt(Exam::getEndTime, LocalDateTime.now())
//                    .eq(Exam::getStatus, Constants.TRUE)
                    .eq(Exam::getStatus, com.oj.common.core.constants.Constants.EXAM_PUBLISHED)
                    .orderByDesc(Exam::getCreateTime));
        } else if (ExamListType.EXAM_HISTORY_LIST.getValue().equals(examListType)) {
            //查询历史竞赛
            examList = examMapper.selectList(new LambdaQueryWrapper<Exam>()
                    .select(Exam::getExamId, Exam::getTitle, Exam::getStartTime, Exam::getEndTime)
                    .le(Exam::getEndTime, LocalDateTime.now())
//                    .eq(Exam::getStatus, Constants.TRUE)
                    .eq(Exam::getStatus, com.oj.common.core.constants.Constants.EXAM_PUBLISHED)
                    .orderByDesc(Exam::getCreateTime));
        }
        if (CollectionUtil.isEmpty(examList)) {
            return ;
        }

        Map<String, Exam> examMap = new HashMap<>();
        List<Long> examIdList = new ArrayList<>();
        for (Exam exam : examList) {
            examMap.put(getDetailKey(exam.getExamId()), exam);
            examIdList.add(exam.getExamId());
        }
        redisService.multiSet(examMap);  //刷新详情缓存
        redisService.deleteObject(getExamListKey(examListType));
        redisService.rightPushAll(getExamListKey(examListType), examIdList);      //刷新列表缓存
    }

    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL_KEY + examId;
    }


    private String getExamListKey(Integer examListType) {
        if (ExamListType.EXAM_UN_FINISH_LIST.getValue().equals(examListType)) {
            return CacheConstants.EXAM_TIME_KEY;
        } else if (ExamListType.EXAM_HISTORY_LIST.getValue().equals(examListType)) {
            return CacheConstants.EXAM_HISTORY_KEY;
        }
        return null;
    }


}
